﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.APP.App_UI.Commons;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.GERP.GFMS.MODEL;

namespace KPIS.GERP.GFMS.APP.App_UI.Forms
{
    public partial class FIM_MoneyReturns : System.Web.UI.Page
    {
        Main main = new Main();
        IBLL_FIM_MoneyRequests mr = new B_FIM_MoneyRequests();
        IBLL_FIM_MoneyRequestDtls mrd = new B_FIM_MoneyRequestDtls();
        IBLL_FIM_MAS_BorrowTypes bt = new B_FIM_MAS_BorrowTypes();
        IBLL_BGM_MAS_Source source = new B_BGM_MAS_Source();
        IBLL_FIM_MAS_Contacts con = new B_FIM_MAS_Contacts();
        IBLL_BGM_MAS_ExpenseGroup expgrp = new B_BGM_MAS_ExpenseGroup();
        IBLL_FIM_MoneyReturns re = new B_FIM_MoneyReturns();
        IBLL_FIM_MoneyReturnDtls red = new B_FIM_MoneyReturnDtls();
        IBLL_HRM_PS_Employees emp = new B_HRM_PS_Employees();
        IBLL_CodeGenerate cg = new B_CodeGenerate();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int year = main.GetSystemControls().default_bgm_year.Value;
                txtBgmYear.Text = year.ToString();
                txtBgmYear.MinValue = year - 10;
                txtBgmYear.MaxValue = year + 1;
                lblBgmYear.Text = year.ToString();

                cmdHistory_Click(null, null);
                //PanelEdit.Height = Unit.Pixel(Convert.ToInt32(Session["Height"]) - 150);
                LoadDdlMoneyRequest();
                LoadDdlEmp();
            }
        }

        protected void LoadDdlMoneyRequest()
        {
            string strBgmYear = txtBgmYear.Text;
            string strEmpSeq = ddlEmp.SelectedValue;
            ddlMoneyRequest.DataSource = mr.GetList(null, strBgmYear, null, "16", null, strEmpSeq, "");//re.LoadMoneyReq();
            ddlMoneyRequest.DataBind();
            ddlMoneyRequest.Items.Insert(0, new RadComboBoxItem("....สัญญายืมทั้งหมด....", ""));
        }

        protected void LoadDdlEmp()
        {
            ddlEmp.DataSource = emp.GetList(null, null, null, "");//re.LoadMoneyReq();
            ddlEmp.DataBind();
            ddlEmp.Items.Insert(0, new RadComboBoxItem("....ผู้ยืมเงินทั้งหมด....", ""));
        }

        protected void txtBgmYear_TextChanged(object sender, EventArgs e)
        {
            LoadGrdViewHistory();

            lblBgmYear.Text = txtBgmYear.Text;
            LoadDdlMoneyRequest();
            ddlEmp.Enabled = true;
        }

        protected void ddlIcmBidSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
           
        }

        protected void cmdArival_Click(object sender, EventArgs e)
        {
            //cmdArival.CssClass = "LinkButtonBigBorder";
            //cmdHistory.CssClass = "LinkButtonBigNoBorder";

            LoadGrdViewArival();
            //GrdViewArival.Visible = true;
            GrdViewHistory.Visible = false;
            PanelEdit.Visible = false;
        }

        protected void cmdHistory_Click(object sender, EventArgs e)
        {
            //cmdArival.CssClass = "LinkButtonBigNoBorder";
            //cmdHistory.CssClass = "LinkButtonBigBorder";

            LoadGrdViewHistory();
            //GrdViewArival.Visible = false;
            GrdViewHistory.Visible = true;
            PanelEdit.Visible = false;
        }

        protected void LoadGrdViewArival()
        {
            //GrdViewArival.DataSource = mr.GetNotReturnList(null, Session["EmpSeq"].ToString(), null);
            //GrdViewArival.DataBind();
        }

        protected void LoadGrdViewHistory()
        {
            string year = txtBgmYear.Text;
            string strEmp = ddlEmp.SelectedValue;
            int? nMoneyReq = main.ConvertNullInt(ddlMoneyRequest.SelectedValue);

            var moneyReturn = re.GetFull(null, year, null, null, strEmp, nMoneyReq, true);

            
            GridSortExpression expression = new GridSortExpression();
            expression.FieldName = "return_seq";
            expression.SetSortOrder("Descending");
            GrdViewHistory.MasterTableView.SortExpressions.AddSortExpression(expression);

            GrdViewHistory.DataSource = moneyReturn;
            GrdViewHistory.DataBind();
        }

        protected void LoadRequestInfo(string strMoneyReqSeq)
        {
            M_FIM_MoneyRequests mrInfo = mr.GetInfo(strMoneyReqSeq);
            lblMoneyReqSeq.Text = strMoneyReqSeq;
            lblMoneyReqName.Text = mrInfo.money_req_name;
            lblBorrowNo.Text = mrInfo.borrow_no;
            lblBorrowReturnDate.Text = (mrInfo.borrow_return_date != null ? mrInfo.borrow_return_date.Value.ToShortDateString() : "-");
            lblRemarks.Text = mrInfo.remarks;
            dpReturnDate.SetValue(DateTime.Today);
            lblReturnRefNo.Text = "-";
            lblReturnSeq.Text = "";

            LoadGrdMoneyReturnDtls();
        }

        protected void LoadReturnInfo(string strMoneyReqSeq, string strReturnSeq)
        {
            M_FIM_MoneyRequests mrInfo = mr.GetInfo(strMoneyReqSeq);
            lblMoneyReqSeq.Text = strMoneyReqSeq;
            lblMoneyReqName.Text = mrInfo.money_req_name;
            lblBorrowNo.Text = mrInfo.borrow_no;
            lblBorrowReturnDate.Text = (mrInfo.borrow_return_date != null ? mrInfo.borrow_return_date.Value.ToShortDateString() : "-");
            lblRemarks.Text = mrInfo.remarks;

            lblReturnSeq.Text = strReturnSeq;
            LoadGrdMoneyReturnDtls();
            //cmdCalculateReturnExchangeRate_Click(null, null);

            GridDataItem item = ((GridDataItem)GrdViewHistory.SelectedItems[0]);
            dpReturnDate.SetValue(main.WebString(item["return_date"].Text));
            lblReturnRefNo.Text = main.WebString(item["ref_no"].Text);
            lblReturnSeq.Text = main.WebString(item["return_seq"].Text);
        }

        protected void LoadGrdMoneyReturnDtls()
        {
            string strReturnSeq = lblReturnSeq.Text;

            var data = red.GetMoneyReqDtls(strReturnSeq, lblMoneyReqSeq.Text);

            grdMoneyReturnDtls.DataSource = data;
            grdMoneyReturnDtls.DataBind();

            if (lblReturnSeq.Text.Length > 0)
            {
                M_FIM_MoneyReturns info = re.GetInfo(lblReturnSeq.Text);
                lblSumReturnBathAmount.Text = (info.total_bath_amount_accum.Value).ToString("#,##0.00");
                lblSumBorrowBathAmount.Text = (info.money_req_total_bath_amount.Value).ToString("#,##0.00");
                lblSumReceiveBathAmount.Text = (info.receive_bath_amount.Value).ToString("#,##0.00");
                lblRemainAmount.Text = ((info.money_req_total_bath_amount ?? 0) - (info.total_bath_amount_accum ?? 0) - (info.receive_bath_amount ?? 0)).ToString("#,###.00");
            }
            else
            {
                M_FIM_MoneyRequests info = mr.GetInfo(lblMoneyReqSeq.Text);
                lblSumReturnBathAmount.Text = (info.return_amount ?? 0).ToString("#,##0.00");
                lblSumBorrowBathAmount.Text = (info.total_amount ?? 0).ToString("#,##0.00");
                lblSumReceiveBathAmount.Text = (info.receive_amount ?? 0).ToString("#,##0.00");
                lblRemainAmount.Text = ((info.total_amount ?? 0) - (info.return_amount ?? 0) - (info.receive_amount ?? 0)).ToString("#,###.00");
            }
        }

        protected void GrdViewArival_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }

        protected void GrdViewArival_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGrdViewArival();
            }
            else if (e.CommandName == "RowClick")
            {
                pnlFillter.Visible = false;
                PanelView.Visible = false;
                PanelEdit.Visible = true;
                Panel3.Visible = false;
                SetControl1("AddNew");

                cmdDelete1.Visible = false;
                LoadRequestInfo(main.WebString(((GridDataItem)e.Item)["money_req_seq"].Text));
            }
        }

        protected void GrdViewHistory_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGrdViewHistory();
            }
            else if (e.CommandName == "RowClick")
            {
                PanelView.Visible = false;
                PanelEdit.Visible = true;
                pnlFillter.Visible = false;
                Panel3.Visible = true;
                SetControl1("Edit");

                LoadReturnInfo(main.WebString(((GridDataItem)e.Item)["money_req_seq"].Text), main.WebString(((GridDataItem)e.Item)["return_seq"].Text));
                lblRecordStatus.Text = main.WebString(((GridDataItem)e.Item)["record_status"].Text);
                bool bApprove = lblRecordStatus.Text == "A";

                cmdApprove.Visible = !bApprove;
                cmdInsert1.Visible = false;
                cmdInsert3.Visible = !bApprove;
                //cmdUnApprove.Visible = bApprove;
                cmdUpdate1.Visible = !bApprove;
                cmdUpdate3.Visible = !bApprove;
                cmdDelete1.Visible = !bApprove;

                grdMoneyReturnDtls.Columns.FindByUniqueName("button").Visible = !bApprove;
            }
        }

        protected void grdMoneyReturnDtls_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }

        protected void grdMoneyReturnDtls_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGrdMoneyReturnDtls();
            }
            else if (e.CommandName == "Select")
            {
                GridDataItem item = (GridDataItem)e.Item;
                lblReqAmount.Text = ((main.ConvertNullDouble(lblRemainAmount.Text) ?? 0) + (main.ConvertNullDouble(main.WebString(item["amount"].Text)) ?? 0)).ToString("#,##0.00");
                lblMoneyReqDtlsSeq.Text = main.WebString(item["money_req_dtls_seq"].Text);
                lblReturnDtlsSeq.Text = main.WebString(item["return_dtls_seq"].Text);
                lblBgmExpName.Text = main.WebString(item["bgm_exp_name"].Text);
                //lblReqCurrencySeq.Text = main.WebString(item["req_currency_seq"].Text);
                txtAmount.Text = main.WebString(item["amount"].Text);

                PanelOpacity.Visible = true;
                PanelMoneyReturnDtls.Visible = true;

                bool bEdit = lblReturnDtlsSeq.Text.Length > 0;
                cmdInsert3.Visible = !bEdit;
                cmdUpdate3.Visible = bEdit;
            }
        }

        //protected void txtAmount_TextChanged(object sender, EventArgs e)
        //{
        //    CalculateAmount();
        //}

        //protected void txtExchangeRate_TextChanged(object sender, EventArgs e)
        //{
        //    CalculateAmount();
        //}

        //protected void CalculateAmount()
        //{
        //    double dAmount = (txtAmount.Text.Length > 0 ? Convert.ToDouble(txtAmount.Text) : 0);
        //    double dExchangeRate = (txtExchangeRate.Text.Length > 0 ? Convert.ToDouble(txtExchangeRate.Text) : 0);

        //    txtBathAmount.Text = (dAmount / dExchangeRate).ToString("#,###.00");
        //}

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            if (!ChackData1())
            {
                main.MessageAlert(Page, "กรอกข้อมูลให้ครบถ้วน");
                return;
            }

            M_FIM_MoneyReturns info = new M_FIM_MoneyReturns();
            M_FIM_MoneyRequests infoMR = mr.GetInfo(lblMoneyReqSeq.Text);
            info.return_date = dpReturnDate.GetDateValue();
            info.money_req_seq = main.ConvertNullInt(lblMoneyReqSeq.Text);
            info.ref_no = cg.GetNextCode(TransectionType.FimMoneyReturnsRefNo, (infoMR.bgm_source_seq ?? 1) == 1 ? "1" : "2");

            info.record_status = "W";
            info.created_by = main.ConvertNullInt(Session["EmpSeq"]);
            info.created_when = DateTime.Today;

            lblReturnSeq.Text = re.Insert(info);

            Panel3.Visible = true;
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            if (!ChackData1())
            {
                main.MessageAlert(Page, "กรอกข้อมูลให้ครบถ้วน");
                return;
            }

            M_FIM_MoneyReturns info = new M_FIM_MoneyReturns();
            M_FIM_MoneyRequests infoMR = mr.GetInfo(lblMoneyReqSeq.Text);
            info.return_seq = Convert.ToInt32(lblReturnSeq.Text);
            info.return_date = dpReturnDate.GetDateValue();
            info.money_req_seq = main.ConvertNullInt(lblMoneyReqSeq.Text);
            info.ref_no = lblReturnRefNo.Text;

            info.record_status = "W";
            info.updated_by = main.ConvertNullInt(Session["EmpSeq"]);
            info.updated_when = DateTime.Today;

            re.Update(info);
        }

        protected void cmdCancel1_Click(object sender, EventArgs e)
        {
            PanelEdit.Visible = false;
            PanelView.Visible = true;
            pnlFillter.Visible = true;
        }

        protected void cmdInsert3_Click(object sender, EventArgs e)
        {
            if (!ChackData3())
            {
                main.MessageAlert(Page, "กรอกข้อมูลให้ครบถ้วน");
                return;
            }

            M_FIM_MoneyReturnDtls info = new M_FIM_MoneyReturnDtls();
            info.return_seq = main.ConvertNullInt(lblReturnSeq.Text);
            info.money_req_dtls_seq = main.ConvertNullInt(lblMoneyReqDtlsSeq.Text);
            info.amount = main.ConvertNullDouble(txtAmount.Text);

            info.record_status = "W";
            info.created_by = main.ConvertNullInt(Session["EmpSeq"]);
            info.created_when = DateTime.Today;
            red.Insert(info);

            PanelMoneyReturnDtls.Visible = false;
            PanelOpacity.Visible = false;
            LoadGrdMoneyReturnDtls();
            //ClearControl3();
        }

        protected void cmdUpdate3_Click(object sender, EventArgs e)
        {
            if (!ChackData3())
            {
                main.MessageAlert(Page, "กรอกข้อมูลให้ครบถ้วน");
                return;
            }

            M_FIM_MoneyReturnDtls info = new M_FIM_MoneyReturnDtls();
            info.return_dtls_seq = Convert.ToInt32(lblReturnDtlsSeq.Text);
            info.return_seq = main.ConvertNullInt(lblReturnSeq.Text);
            info.money_req_dtls_seq = main.ConvertNullInt(lblMoneyReqDtlsSeq.Text);
            info.amount = main.ConvertNullDouble(txtAmount.Text);
            info.req_amount = main.ConvertNullDouble(lblReqAmount.Text);

            info.updated_by = main.ConvertNullInt(Session["EmpSeq"]);
            info.updated_when = DateTime.Today;

            //int? intAmount = main.ConvertNullInt(txtAmount.Text);
            //int? intReqAmount = main.ConvertNullInt(lblReqAmount.Text);
            //if (intAmount > intReqAmount)
            //{
            //    main.MessageAlert(Page, "จำนวนเงินหักล้างต้องไม่มากกว่าจำนวนเงินขอยืม");
            //    txtAmount.Text = "";
            //}

            red.Update(info);

            PanelMoneyReturnDtls.Visible = false;
            PanelOpacity.Visible = false;
            LoadGrdMoneyReturnDtls();
            //ClearControl3();
        }

        protected void cmdCancel3_Click(object sender, EventArgs e)
        {
            PanelMoneyReturnDtls.Visible = false;
            PanelOpacity.Visible = false;
            //ClearControl3();
        }
        
        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            M_FIM_MoneyReturns info = new M_FIM_MoneyReturns();
            info.return_seq = Convert.ToInt32(lblReturnSeq.Text);
            info.updated_by = main.ConvertNullInt(Session["EmpSeq"]);
            info.updated_when = DateTime.Now;

            re.Delete(info);
            PanelEdit.Visible = false;
            PanelView.Visible = true;
            pnlFillter.Visible = true;
            LoadGrdViewHistory();
            main.MessageAlert(Page, "ลบรายการเรียบร้อย");
        }

        //protected void CalculateToAmount(object sender, EventArgs e)
        //{
        //    //double dFromAmount = (txtFromAmount.Text.Length > 0 ? Convert.ToDouble(txtFromAmount.Text) : 1);
        //    //double dExchangeRate = (txtExExchangeRate.Text.Length > 0 ? Convert.ToDouble(txtExExchangeRate.Text) : 1);

        //    txtToAmount.Text = (dFromAmount / dExchangeRate).ToString("#,###.00");
        //}

        protected void SetControl1(string strCommand)
        {
            switch (strCommand)
            {
                case "AddNew":
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;

                    lblModeName.Text = "เพิ่มรายการใหม่";
                    lblMode.Text = "A";
                    break;
                case "Edit":
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;
                    //cmdAddNew2.Visible = true;

                    lblModeName.Text = "แก้ไขรายการ";
                    lblMode.Text = "E";
                    break;
            }
        }

        protected bool ChackData1()
        {
            bool bOK = true;

            if (dpReturnDate.GetDateValue() == null)
            {
                bOK = false;
            }

            return bOK;
        }


        protected bool ChackData3()
        {
            bool bOK = true;

            if (txtAmount.Text.Length == 0)
            {
                bOK = false;
            }

            return bOK;
        }

        //protected void RepeaterReturnExpense_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item is RepeaterItem)
        //    {
        //        if (e.Item.ItemType != ListItemType.Header)
        //        {
        //            RepeaterItem item = (RepeaterItem)e.Item;
        //            Label lblReturnDtlsSeq = (Label)item.FindControl("lblReturnDtlsSeq");
        //            Repeater RepeaterReturnExchange = (Repeater)item.FindControl("RepeaterReturnExchange");

        //            M_FIM_MoneyReturnDtls info = red.GetInfo(lblReturnDtlsSeq.Text);

        //            DataTable dtb = new DataTable();
        //            dtb.Columns.Add("from_currency_name_short", typeof(string));
        //            dtb.Columns.Add("to_currency_name_short", typeof(string));
        //            dtb.Columns.Add("exchange_rate", typeof(double));
        //            dtb.Columns.Add("from_amount", typeof(double));
        //            dtb.Columns.Add("to_amount", typeof(double));

        //            RepeaterReturnExchange.DataSource = CalculateExchange(dtb, info.amount.Value, info.currency_seq.ToString());
        //            RepeaterReturnExchange.DataBind();
        //        }
        //    }
        //}

        //protected void RepeaterReturnExchange_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item is RepeaterItem)
        //    {
        //        if (e.Item.ItemType == ListItemType.Footer)
        //        {
        //            Label lblBathAmount = (Label)e.Item.FindControl("lblBathAmount");
        //            lblBathAmount.Text = Convert.ToDouble(ViewState["bath_amount"]).ToString("#,###.00");

        //            string strReturnDtlsSeq = ((Label)((RepeaterItem)e.Item.Parent.Parent).FindControl("lblReturnDtlsSeq")).Text;
        //            red.UpdateBathAmount(strReturnDtlsSeq, Convert.ToDouble(ViewState["bath_amount"]).ToString());
        //        }
        //    }
        //}

        protected void cmdManual_Click(object sender, EventArgs e)
        {
            LinkButton cmd = (LinkButton)sender;
            RepeaterItem item = (RepeaterItem)((LinkButton)sender).Parent;
            RadNumericTextBox txtExchangeRate2 = (RadNumericTextBox)item.FindControl("txtExchangeRate");

            txtExchangeRate2.Text = cmd.Text;
            cmd.Visible = false;
            txtExchangeRate2.Visible = true;
        }

        protected void cmdApprove_Click(object sender, EventArgs e)
        {
            re.ChangeStatus(lblReturnSeq.Text, "A", Session["EmpSeq"].ToString());

            
            //double dReceiveAmount = Convert.ToDouble(strBathAmount);
            ////IList<M_FIM_MoneyRequestDtls> mrdInfo = mrd.GetFull(strMoneyReqSeq);
            //IList<M_FIM_MoneyReturnDtls> retdInfo = retd.GetMoneyReqDtls(null, strMoneyReqSeq);

            //for (int i = 0; i < retdInfo.Count; i++)
            //{
            //    double dReqAmount = (retdInfo[i].req_bath_amount != null ? retdInfo[i].req_bath_amount.Value : 0);
            //    double dReturnAmount = (retdInfo[i].bath_amount.Value != null ? retdInfo[i].bath_amount.Value : 0);
            //    double dRemainAmount = dReqAmount - dReturnAmount;

            //    if (dRemainAmount > 0)
            //    {
            //        double dUsedAmount = 0;

            //        if (dRemainAmount < dReceiveAmount)
            //        {
            //            dUsedAmount = dRemainAmount;
            //            dReceiveAmount -= dRemainAmount;
            //        }
            //        else
            //        {
            //            dUsedAmount = dReceiveAmount;
            //            dReceiveAmount = 0;
            //        }

            //        M_BGM_ProjectOperationReport reportInfo = new M_BGM_ProjectOperationReport();
            //        reportInfo.bgm_proj_oper_exp_seq = mrd.GetInfo(retdInfo[i].money_req_dtls_seq.ToString()).bgm_proj_oper_exp_seq;
            //        //reportInfo.money_req_dtls_seq = expInfo.money_req_dtls_seq;
            //        reportInfo.used_amount = info.bath_amount;
            //        reportInfo.cal_amount = info.bath_amount;
            //        reportInfo.rpt_type = 8;//คืนเงิน
            //        reportInfo.created_by = main.ConvertNullInt(strUserID);
            //        reportInfo.created_when = DateTime.Now;

            //        oprpt.Insert(reportInfo);
            //    }
            //}

            PanelView.Visible = true;
            pnlFillter.Visible = true;
            PanelEdit.Visible = false;
            LoadGrdViewHistory();
            main.MessageAlert(Page, "บันทึกเรียบร้อยแล้ว");
        }

        protected void cmdUnApprove_Click(object sender, EventArgs e)
        {
            re.ChangeStatus(lblReturnSeq.Text, "W", Session["EmpSeq"].ToString());
            PanelView.Visible = true;
            pnlFillter.Visible = true;
            PanelEdit.Visible = false;
            LoadGrdViewHistory();
            main.MessageAlert(Page, "บันทึกเรียบร้อยแล้ว");
        }

        protected void ddlEmp_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDdlMoneyRequest();
            LoadGrdViewHistory();
        }

        protected void ddlMoneyReturns_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string strMoneyReqSeq = ddlMoneyRequest.SelectedValue;

            if (!string.IsNullOrEmpty(strMoneyReqSeq))
            {
                string strEmpSeq = mr.GetInfo(strMoneyReqSeq).emp_seq.ToString();
                ddlEmp.SelectedValue = strEmpSeq;
                ddlEmp.Enabled = false;
            }
            else
            {
                ddlEmp.Enabled = true;
            }

            LoadGrdViewHistory();
        }

        protected void GrdViewHistory_ItemDataBound1(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem dataBoundItem = e.Item as GridDataItem;
                var strStatus = dataBoundItem["record_status"].Text;
                var strSeq = dataBoundItem["return_seq"].Text;
                GridDataItem cmditem = (GridDataItem)e.Item;
                LinkButton lbtnadd = (LinkButton)cmditem.FindControl("cmdPrintPopup");
                lbtnadd.Visible = false;
                if (strStatus.StartsWith("A"))
                {
                    if (!String.IsNullOrEmpty(strSeq))
                    {
                        var lst = re.GetInfo(strSeq);//acm.GetListDetail(Convert.ToInt32(strSeq), "fim_payments");
                        if (lst != null)
                            lbtnadd.Visible = true;
                    }
                }
            }
        }

     }
}